package com.lanou.moving.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bartoszlipinski.flippablestackview.FlippableStackView;
import com.bartoszlipinski.flippablestackview.StackPageTransformer;
import com.lanou.moving.R;
import com.lanou.moving.activity.GuessYouLikeActivity;
import com.lanou.moving.activity.MVPlayActivity;
import com.lanou.moving.activity.SingerDetailActivity;
import com.lanou.moving.activity.SongListDetailActivity;
import com.lanou.moving.adapter.FlippableStackViewAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.RecommendRoot;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by 梁树珍 on 15/10/20.
 */
public class RecommendFragment extends BaseFragment implements FlippableStackViewAdapter.ToSkipListener {
    private Context context;
    private FlippableStackView stackView;
    private FlippableStackViewAdapter stackViewAdapter;
    private ArrayList<RecommendRoot.DataEntity> dataEntityArrayList;
    private GsonRequest<RecommendRoot> gsonRequest;
    private VolleySingleton volleySingleton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, null);
        stackView = (FlippableStackView) view.findViewById(R.id.stackView);
        volleySingleton = VolleySingleton.getInstance(context);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
    }

    public void getData() {
        gsonRequest = new GsonRequest<>(Request.Method.GET, URL.URL_RECOMMEND, RecommendRoot.class, new Response.Listener<RecommendRoot>() {
            @Override
            public void onResponse(RecommendRoot response) {
                dataEntityArrayList = (ArrayList<RecommendRoot.DataEntity>) response.getData();
                stackViewAdapter = new FlippableStackViewAdapter(mContext, dataEntityArrayList);
                stackViewAdapter.setToSkipListener(RecommendFragment.this);
                // 初始化stack的层数以及方向
                stackView.initStack(dataEntityArrayList.size(), StackPageTransformer.Orientation.VERTICAL);
                stackView.setAdapter(stackViewAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(gsonRequest);
    }

//    public void changeFragment(int position) {
//        FragmentManager fm = getFragmentManager();
//        FragmentTransaction ft = fm.beginTransaction();
//        RecommendSongsFragment recommendSongsFragment = new RecommendSongsFragment();
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("recommend", dataEntityArrayList);
//        bundle.putInt("position", position);
//        recommendSongsFragment.setArguments(bundle);
//        ft.replace(android.R.id.content, recommendSongsFragment).addToBackStack(null);
//        ft.commit();
//    }

    @Override
    public void onClick(RecommendRoot.DataEntity dataEntity) {
        switch (dataEntity.getAction().getType()) {
            case 5:
                Intent intent5 = new Intent(mContext, SongListDetailActivity.class);
                intent5.putExtra("id", dataEntity.getId());
                startActivity(intent5);
                break;
            case 13:
                Intent intent13 = new Intent(mContext, GuessYouLikeActivity.class);
                intent13.putExtra("id", dataEntity.getId());
                intent13.putExtra("pic_url", dataEntity.getSmallPicUrls().get(0));
                intent13.putExtra("name", dataEntity.getName());//标题
                intent13.putExtra("from", 13);//数据来源
                startActivity(intent13);
                break;
            case 18:
                Intent intent18 = new Intent(mContext, MVPlayActivity.class);
                intent18.putExtra("videoId", dataEntity.getId());
                startActivity(intent18);
                break;
            case 20:
                Intent intent20 = new Intent(mContext, SingerDetailActivity.class);
                intent20.putExtra("singer_id", dataEntity.getId());
                startActivity(intent20);
                break;
            case 21:
                Intent intent21 = new Intent(mContext, GuessYouLikeActivity.class);
                intent21.putExtra("id", dataEntity.getId());
                intent21.putExtra("pic_url", dataEntity.getPicUrl());
                intent21.putExtra("name", dataEntity.getName());
                intent21.putExtra("from", 21);//数据来源
                startActivity(intent21);
                break;
            case 22:
                Intent intent22 = new Intent(mContext, GuessYouLikeActivity.class);
                intent22.putExtra("pic_url", dataEntity.getPicUrl());
                intent22.putExtra("from", 22);//数据来源
                startActivity(intent22);
                break;
        }
    }
}
